<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>消息滚动切换</title>
  <style>
    .tip-bar {
      display: inline-flex;
      align-items: center;
      height: 30px;
      background-color: rgba(0,0,0,.4);
      border-radius: 16px;
    }
    img {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      margin-right: 5px;
    }
    span {
      font-size: 13px;
      color: white;
      margin-right: 8px;
    }
  </style>
</head>
<body>
  
  
  <div class="tip-bar">
    <img src="https://bfs.biyao.com/group1/M01/A2/67/rBACVGA_iOuAYaTxAAAPbted3yE165.png" alt="">
    <span>183***138对这件商品感兴趣</span>
  </div>

  <script>
    // 1.从服务器拿到数据ajax/fetch请求
    let tipList = [
      {
        icon: 'https://bfs.biyao.com/group1/M01/A6/97/rBACYWBCHqyAFH5tAAANZXX5Eww646.png',
        title: 'coderwhy对这件商品感兴趣'
      },
      {
        icon: 'https://bfs.biyao.com/group1/M01/A2/67/rBACVGA_iOuAYaTxAAAPbted3yE165.png',
        title: '123***814对这件商品感兴趣'
      },
      {
        icon: 'https://bfs.biyao.com/group1/M00/7F/4E/rBACYV16HseAP-PnAAAW9bbVoKE463.png',
        title: '刘军对这件商品感兴趣'
      }
    ]

    // 2. 获取元素
    let divEl = document.querySelector(".tip-bar");
    let imgEl = divEl.querySelector("img");
    let spanEl = divEl.querySelector("span");
    // 3. 循环将数据赋予元素
    let arrLen = tipList.length;
    var i = 0;
    setInterval(function(){
      if(i === 3) i = 0;
      imgEl.src = tipList[i].icon;
      spanEl.textContent = tipList[i].title;
      i++;
    }, 3000);
    // while(arrLen === 3){
    //   console.log(arrLen);
    //   tipList.forEach((item, index, arr) => {
    //   setTimeout(() => {
    //     imgEl.src = item.icon;
    //     spanEl.textContent = item.title;
    //     console.log(imgEl, spanEl);
    //     arrLen = index+1;
    //     console.log(arrLen);
    //   }, 3000);
    // });
    // }

  </script>

</body>
</html>